﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.PetroleumDataPartLib
{
    /// <summary>
    /// Interaction logic for PartOilTank.xaml
    /// </summary>
    [System.ComponentModel.DisplayName("管网油罐")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.PetroleumDataPartLib;component/Images/PartIcons/管网油罐.png")]
    public partial class PartOilTankForGw : DataItemPart
    {
        Storyboard _storyboard;

        #region Property

        public static readonly DependencyProperty ValueProperty =
           DependencyProperty.Register("Value", typeof(double), typeof(PartOilTankForGw),
           new PropertyMetadata(double.NaN, new PropertyChangedCallback(OnValuePropertyChange)));

        public static readonly DependencyProperty CapacityFillBrushProperty =
          DependencyProperty.Register("CapacityFillBrush", typeof(BrushEx), typeof(PartOilTankForGw),
          new PropertyMetadata(OnCapacityFillBrushPropertyChange));

        private double _XMaximum;
        private double _XMinimum;
        private Thickness _XCapacityRangeOffset;
        private ImageSourceFile _XTankBackgroundImage;
        private BrushEx _XCapacityEmptyBrush;
        private BrushEx _XTopFillBrush;
        private BrushEx _XBodyFillBrush;
        private BrushEx _XBottomFillBrush;
        private string _XTextFormat;
        private double? _XFontSize;
        private TextAlignment _XTextAlign;
        private HorizontalAlignment _XTextHAlign;
        private VerticalAlignment _XTextVAlign;
        private Thickness _XTextMargin;
        private System.Windows.Media.FontFamily _XFont;
        private System.Windows.FontWeight _XFontWeight;
        private Thickness _XTopMargin;
        private Thickness _XBodyMargin;
        private Thickness _XBottomMargin;
        private double? _XOrginalHeight;
        private double? _XOrginalWidth;
        private double? _XTopFillHeight;
        private double? _XBottomFillHeight;
        private double? _XTankWidth;
        private double? _XTankHeight;
        #endregion

        #region XProperty

        [Category("Data")]
        [DisplayName("最大值")]
        [Description("Maximum")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "Maximum", "最大值")]
        public double XMaximum
        {
            get
            {
                return _XMaximum;
            }
            set
            {
                _XMaximum = value;
                Refresh();
            }
        }

        [Category("Data")]
        [DisplayName("最小值")]
        [Description("Minimum")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "Minimum", "最小值")]
        public double XMinimum
        {
            get
            {
                return _XMinimum;
            }
            set
            {
                _XMinimum = value;
                Refresh();
            }
        }

        [Category("Data")]
        [DisplayName("当前值")]
        [Description("Value")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "Value", "当前值")]
        public double XValue
        {
            get
            {
                return (double)GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }
        [Category("Data")]
        [DisplayName("罐容区域的限定值")]
        [Description("CapacityRangeOffset")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "CapacityRangeOffset", "罐容区域的限定值")]
        public Thickness XCapacityRangeOffset
        {
            get
            {
                return _XCapacityRangeOffset;
            }
            set
            {
                _XCapacityRangeOffset = value;
                Refresh();
            }
        }
        [Category("Data")]
        [DisplayName("油罐背景图")]
        [Description("TankBackgroundImage")]
        [Editor(typeof(UI.XProperty.Editors.XImageSourceFileEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "TankBackgroundImage", "油罐背景图")]
        public ImageSourceFile XTankBackgroundImage
        {
            get
            {
                return _XTankBackgroundImage;
            }
            set
            {
                _XTankBackgroundImage = value;
                Refresh();
            }
        }

        /// <summary>
        /// 空罐区域颜色
        /// </summary>
        [Category("Data")]
        [DisplayName("空罐区域颜色")]
        [Description("CapacityEmptyBrush")]
        [Editor(typeof(UI.XProperty.Editors.XBrushEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "CapacityEmptyBrush", "空罐区域颜色")]
        public BrushEx XCapacityEmptyBrush
        {
            get
            {
                return _XCapacityEmptyBrush;
            }
            set
            {
                _XCapacityEmptyBrush = value;
                Refresh();
            }
        }

        [Category("Data")]
        [DisplayName("非空罐区域颜色")]
        [Description("CapacityFillBrush")]
        [Editor(typeof(UI.XProperty.Editors.XBrushEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "CapacityFillBrush", "非空罐区域颜色")]
        public BrushEx XCapacityFillBrush
        {
            get
            {
                return (BrushEx)GetValue(CapacityFillBrushProperty);
            }
            set
            {
                SetValue(CapacityFillBrushProperty, value);
            }
        }

        [Category("Data")]
        [DisplayName("罐盖颜色")]
        [Description("TopFillBrush")]
        [Editor(typeof(UI.XProperty.Editors.XBrushEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "TopFillBrush", "罐盖颜色")]
        public BrushEx XTopFillBrush
        {
            get
            {
                return _XTopFillBrush;
            }
            set
            {
                _XTopFillBrush = value;
                Refresh();
            }
        }
        [Category("Data")]
        [DisplayName("进度颜色")]
        [Description("BodyFillBrush")]
        [Editor(typeof(UI.XProperty.Editors.XBrushEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "BodyFillBrush", "进度颜色")]
        public BrushEx XBodyFillBrush
        {
            get
            {
                return _XBodyFillBrush;
            }
            set
            {
                _XBodyFillBrush = value;
                Refresh();
            }
        }

        [Category("Data")]
        [DisplayName("罐底颜色")]
        [Description("BottomFillBrush")]
        [Editor(typeof(UI.XProperty.Editors.XBrushEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "BottomFillBrush", "罐底颜色")]
        public BrushEx XBottomFillBrush
        {
            get
            {
                return _XBottomFillBrush;
            }
            set
            {
                _XBottomFillBrush = value;
                Refresh();
            }
        }
        [Category("其他")]
        [DisplayName("文本格式化")]
        [Description("TextFormat")]
        [Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextFormat", "文本格式化")]
        public string XTextFormat
        {
            get
            {
                return _XTextFormat;
            }
            set
            {
                _XTextFormat = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体大小")]
        [Description("FontSize")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "FontSize", "字体大小")]
        public double? XFontSize
        {
            get
            {
                return _XFontSize;
            }
            set
            {
                _XFontSize = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体水平对齐")]
        [Description("TextAlign")]
        [Editor(typeof(UI.XProperty.Editors.XTextAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextAlign", "字体位置")]
        public TextAlignment XTextAlign
        {
            get
            {
                return _XTextAlign;
            }
            set
            {
                _XTextAlign = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体水平位置")]
        [Description("TextHAlign")]
        [Editor(typeof(UI.XProperty.Editors.XHorizontalAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextHAlign", "字体水平位置")]
        public HorizontalAlignment XTextHAlign
        {
            get
            {
                return _XTextHAlign;
            }
            set
            {
                _XTextHAlign = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体垂直位置")]
        [Description("TextVAlign")]
        [Editor(typeof(UI.XProperty.Editors.XVerticalAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextVAlign", "字体垂直位置")]
        public VerticalAlignment XTextVAlign
        {
            get
            {
                return _XTextVAlign;
            }
            set
            {
                _XTextVAlign = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("文本边距")]
        [Description("TextMargin")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextMargin", "文本边距")]
        public Thickness XTextMargin
        {
            get
            {
                return _XTextMargin;
            }
            set
            {
                _XTextMargin = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体样式")]
        [Description("Font")]
        [Editor(typeof(UI.XProperty.Editors.XFontFamilyEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Font", "字体样式")]
        public FontFamily XFont
        {
            get
            {
                return _XFont;
            }
            set
            {
                _XFont = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("字体粗细")]
        [Description("FontWeight")]
        [Editor(typeof(UI.XProperty.Editors.XFontWeightEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "FontWeight", "字体粗细")]
        public FontWeight XFontWeight
        {
            get
            {
                return _XFontWeight;
            }
            set
            {
                _XFontWeight = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶盖边距")]
        [Description("TopMargin")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TopMargin", "桶盖边距")]
        public Thickness XTopMargin
        {
            get
            {
                return _XTopMargin;
            }
            set
            {
                _XTopMargin = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶体边距")]
        [Description("BodyMargin")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "BodyMargin", "桶体边距")]
        public Thickness XBodyMargin
        {
            get
            {
                return _XBodyMargin;
            }
            set
            {
                _XBodyMargin = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶低边距")]
        [Description("BottomMargin")]
        [Editor(typeof(UI.XProperty.Editors.XThicknessEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "BottomMargin", "桶低边距")]
        public Thickness XBottomMargin
        {
            get
            {
                return _XBottomMargin;
            }
            set
            {
                _XBottomMargin = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶背景图片高度")]
        [Description("OriginalHeight")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "OriginalHeight", "桶背景图片高度")]
        public double? XOrginalHeight
        {
            get
            {
                return _XOrginalHeight;
            }
            set
            {
                _XOrginalHeight = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶背景图片宽度")]
        [Description("OriginalWidth")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "OriginalWidth", "桶背景图片宽度")]
        public double? XOrginalWidth
        {
            get
            {
                return _XOrginalWidth;
            }
            set
            {
                _XOrginalWidth = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶盖高度")]
        [Description("TopFillHeight")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TopFillHeight", "桶盖高度")]
        public double? XTopFillHeight
        {
            get
            {
                return _XTopFillHeight;
            }
            set
            {
                _XTopFillHeight = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶底高度")]
        [Description("BottomFillHeight")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "BottomFillHeight", "桶底高度")]
        public double? XBottomFillHeight
        {
            get
            {
                return _XBottomFillHeight;
            }
            set
            {
                _XBottomFillHeight = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶宽度")]
        [Description("TankWidth")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TankWidth", "桶宽度")]
        public double? XTankWidth
        {
            get
            {
                return _XTankWidth;
            }
            set
            {
                _XTankWidth = value;
                Refresh();
            }
        }

        [Category("其他")]
        [DisplayName("桶高度")]
        [Description("TankHeight")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TankHeight", "桶高度")]
        public double? XTankHeight
        {
            get
            {
                return _XTankHeight;
            }
            set
            {
                _XTankHeight = value;
                Refresh();
            }
        }
        #endregion

        public PartOilTankForGw()
        {
            XMaximum = 120;
            XMinimum = 0;
            XDataItemId = "data1";
            XDataSeriesId = "series1";
            XTextFormat = "{data.Value:0}%";
             SetDefaultDataResult();
            InitializeComponent();
        }
        private void SetDefaultDataResult()
        {
            Random random = new Random();
            ServiceProxy.DataServiceRef.DataSeries[] series = new ServiceProxy.DataServiceRef.DataSeries[1];
            ServiceProxy.DataServiceRef.DataItem[] tmpItems = new ServiceProxy.DataServiceRef.DataItem[1];
            tmpItems[0] = new ServiceProxy.DataServiceRef.DataItem();
            tmpItems[0].Id = "data1";
            tmpItems[0].Name = "data1";
            tmpItems[0].Value = (random.NextDouble() * 120).ToString("F2");

            series[0] = new ServiceProxy.DataServiceRef.DataSeries();
            series[0].Id = "series1";
            series[0].Name = "series1";
            series[0].DataItems = tmpItems;
            DataResult = new ServiceProxy.DataServiceRef.DataResult();
            DataResult.DataSeries = series;
        }

        protected override void OnInit(PartContext context)
        {
            base.OnInit(context);

            XMaximum = 1.0f;
            XMinimum = 0.0f;
            XValue = 0.5f;

            XCapacityRangeOffset = new Thickness(20, 60, 20, 10);

            XCapacityFillBrush = new BrushEx(new SolidColorBrush(Colors.Red),"");

            XFont = new FontFamily("微软雅黑");
        }
        protected override void OnApplyXProperty()
        {
            if (bgRec == null)
            {
                return;
            }
            base.OnApplyXProperty();

            if (XCapacityEmptyBrush != null)
            {
                bgRec.Fill = XCapacityEmptyBrush.Brush;
            }
            if (XTopFillBrush != null)
            {
                topEllip.Fill = XTopFillBrush.Brush;
            }
            else
            {
                topEllip.Fill = XCapacityFillBrush == null ? null : XCapacityFillBrush.Brush;
            }
            if (XBodyFillBrush != null)
            {
                centerRect.Fill = XBodyFillBrush.Brush;
            }
            else
            {
                centerRect.Fill = XCapacityFillBrush == null ? null : XCapacityFillBrush.Brush;
            }
            if (XBottomFillBrush != null)
            {
                bottomEllip.Fill = XBottomFillBrush.Brush;
            }
            else
            {
                bottomEllip.Fill = XBodyFillBrush == null ? null : XBodyFillBrush.Brush;
            }
            topEllip.Margin = XTopMargin;
            _ctlBody.Margin = XBodyMargin;
            bottomEllip.Margin = XBottomMargin;
            if (XTankBackgroundImage != null)
            {
                bgImage.Source = XTankBackgroundImage.ImageSource;
            }
            if (XTopFillHeight.HasValue)
            {
                topEllip.Height = XTopFillHeight.Value;
            }
            if (XBottomFillHeight.HasValue)
            {
                bottomEllip.Height = XBottomFillHeight.Value;
            }
            if (XFontSize != null)
            {
                tbText.FontSize = XFontSize.Value;
            }
            tbText.TextAlignment = XTextAlign;
            tbText.HorizontalAlignment = XTextHAlign;
            tbText.VerticalAlignment = XTextVAlign;
            if (XFont != null)
            {
                tbText.FontFamily = XFont;
            }
            if (XForeground != null)
            {
                tbText.Foreground = XForeground.Brush;
            }
            tbText.FontWeight = XFontWeight;
            tbText.Margin = XTextMargin;
        }
        protected override void OnActive(PartContext context)
        {
            base.OnActive(context);
        }

        protected override void OnDeactive(PartContext context)
        {
            if (_storyboard != null)
            {
                _storyboard.Stop();
                _storyboard.Remove();
                _storyboard = null;
            }

            base.OnDeactive(context);
        }

        protected override void OnDestory()
        {
            if (_storyboard != null)
            {
                _storyboard.Stop();
                _storyboard.Remove();
                _storyboard = null;
            }
            base.OnDestory();
        }

        /// <summary>
        /// 更新显示
        /// </summary>
        public void RefreshDraw()
        {
            if (this.DataItem != null)
            {
                this.tbText.Text = this.FormartText(this.DataItem);

                if (this.ActualWidth > 0 && this.ActualHeight > 0 && bgImage.Source != null)
                {
                    double tankActualWidth = XTankWidth.HasValue ? XTankWidth.Value : this.ActualWidth;
                    double tankActualHeight = XTankHeight.HasValue ? XTankHeight.Value : this.ActualWidth;

                    double originalHeight = XOrginalHeight.HasValue ? XOrginalHeight.Value : bgImage.ActualHeight;
                    double originalWidth = XOrginalWidth.HasValue ? XOrginalWidth.Value : bgImage.ActualWidth;
                    double curOffsetTop = (tankActualHeight / originalHeight) * this.XCapacityRangeOffset.Top;
                    double curOffsetBottom = (tankActualHeight / originalHeight) * this.XCapacityRangeOffset.Bottom;
                    double curOffsetLeft = (tankActualWidth / originalWidth) * this.XCapacityRangeOffset.Left;
                    double curOffsetRight = (tankActualWidth / originalWidth) * this.XCapacityRangeOffset.Right;

                    bgRec.Margin = new Thickness(curOffsetLeft, curOffsetTop, curOffsetRight, curOffsetBottom);
                    progressRec.Margin = new Thickness(curOffsetLeft, curOffsetTop, curOffsetRight, curOffsetBottom);

                    double scale = (XValue - XMinimum) / (XMaximum - XMinimum);
                    if (scale > 1.0d)
                    {
                        scale = 1.0d;
                    }
                    if((bgRec.ActualHeight - topEllip.ActualHeight) > 0)
                    {
                        _ctlBody.Height = (bgRec.ActualHeight - topEllip.ActualHeight) * scale;
                    }
                }
            }
        }

        protected override void OnDataItemBinding(CPPEI.Coolzon.ServiceProxy.DataServiceRef.DataItem dataItem)
        {
            if (_storyboard != null)
            {
                _storyboard.Stop();
                _storyboard.Remove();
                _storyboard = null;
            }

            _storyboard = new Storyboard();

            DoubleAnimation doubleAnimation = new DoubleAnimation(XMinimum, Convert.ToDouble(dataItem.Value), new Duration(TimeSpan.FromSeconds(XDataAnimationDuration)));
            //this.BeginAnimation(ValueProperty, doubleAnimation);
            Storyboard.SetTarget(doubleAnimation, this);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(PartOilTankForGw.ValueProperty));
            _storyboard.Children.Add(doubleAnimation);
            _storyboard.Begin();

        }

        private static void OnValuePropertyChange(DependencyObject send, DependencyPropertyChangedEventArgs e)
        {
            PartOilTankForGw pot = send as PartOilTankForGw;
            if ((double)e.NewValue < pot.XMinimum) return;
            pot.RefreshDraw();
        }

        private static void OnCapacityFillBrushPropertyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PartOilTankForGw pot = d as PartOilTankForGw;

            if (e.NewValue != null)
            {
                pot.centerRect.Fill = (e.NewValue as BrushEx).Brush;
                pot.topEllip.Fill = (e.NewValue as BrushEx).Brush;
                pot.bottomEllip.Fill = (e.NewValue as BrushEx).Brush;
            }
        }

        private string FormartText(ServiceProxy.DataServiceRef.DataItem dataItem)
        {
            if (XTextFormat == null)
            {
                CPPEI.Coolzon.Common.Logger.WarningToTag("PartOilTankForGw", "格式未配置。");
                return string.Empty;
            }
            Regex r = new Regex(@"{[^{}]*}");
            MatchCollection matchs = r.Matches(XTextFormat);
            string newText = XTextFormat;
            foreach (Match match in matchs)
            {
                string matchResult = match.Value.Trim(new char[] { '{', '}' });
                if (matchResult.StartsWith("data"))
                {
                    string[] values = matchResult.Replace("data.", "").Split(new string[] { ":" }, StringSplitOptions.None);
                    PropertyInfo[] propertyInfos = dataItem.GetType().GetProperties();
                    for (int i = 0; i < propertyInfos.Length; i++)
                    {
                        string name = propertyInfos[i].Name.ToUpper();
                        if (values[0].ToUpper().Equals(name))
                        {
                            object obj = propertyInfos[i].GetValue(dataItem);

                            if (values.Length > 1)
                            {
                                double res = default(double);
                                if (double.TryParse(obj.ToString(), out res))
                                {
                                    newText = newText.Replace(match.Value, res.ToString(values[1]));
                                    break;
                                }
                            }
                            newText = newText.Replace(match.Value, obj.ToString());
                            break;
                        }
                    }
                }
                else if (matchResult.StartsWith("@"))
                {
                    string value = matchResult.TrimStart(new char[] { '@' });
                    Metadata.Parameter parameter = this.GetParameter(value);
                    if (parameter != null)
                    {
                        newText = newText.Replace(match.Value, value);
                    }
                }
            }
            return newText.Replace("\\n", "\n");
        }
    }
}
